//
//  SSNetworkTools.swift
//  SSSina
//
//  Created by ss on 2017/7/2.
//  Copyright © 2017年 ss. All rights reserved.
//


import UIKit
import AFNetworking
// 请求方式get post 枚举
// 支持多种类型 不单单是int类型
enum SSNetworkToolsType:String {
    case get = "get"
    case post = "post"
}


class SSNetworkTools: AFHTTPSessionManager {
    
    
    
    // 全局访问点
    static let share: SSNetworkTools = {
        let tools = SSNetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    /// 网络请求公共方法封装
    ///
    /// - Parameters:
    ///   - type: 请求方式
    ///   - urlString: 地址
    ///   - parameters: 参数
    ///   - success: 成功的回调
    ///   - failure: 失败的回调
    func request(type: SSNetworkToolsType, urlString: String, parameters: Any?, success:@escaping (Any?)->(), failure:@escaping (Error)->()){
        // 判断请求方式
        // get
        if type == .get {
            get(urlString, parameters: parameters, progress: nil, success: { (_, response) in
                success(response)
            }, failure: { (_, error) in
                failure(error)
            })
            
        }else{
            // post
            post(urlString, parameters: parameters, progress: nil, success: { (_, response) in
                success(response)
            }, failure: { (_, error) in
                failure(error)
            })
        }
    }
}

//OAuth需要的网络接口
extension SSNetworkTools{
    //请求userAccount
    func oauthLoadUserAccout(code:String,success:@escaping (Any?)->(), failure:@escaping (Error)->()){
        //请求的地址 请求token的地址
        let urlString = "https://api.weibo.com/oauth2/access_token"
        //请求参数
        let params = [
            "client_id":SSAppKey,
            "client_secret":SSAppSecret,
            "grant_type":"authorization_code",
            "code": code  ,
            "redirect_uri":SSREDIRECT_URI,
            ]
        request(type:SSNetworkToolsType.post, urlString: urlString, parameters: params, success: success, failure: failure)
    }
    
    //请求个人信息数据
    func oauthLoadUserInfo(userAccountModel:SSUserAccountModel,success:@escaping (Any?)->(), failure:@escaping (Error)->()){
        //请求地址
        let urlString = "https://api.weibo.com/2/users/show.json"
        //请求参数
        let params = [
            
            "access_token":userAccountModel.access_token,
            "uid":userAccountModel.uid
        ]
        //发送网络请求
        request(type: SSNetworkToolsType.get, urlString: urlString, parameters: params, success: success, failure: failure)
    }
    
}

//请求首页数据接口
extension SSNetworkTools{
    func homeLoadData(since_id:Int64, max_id: Int64, success:@escaping (Any?)->(), failure:@escaping (Error)->()){
        // 请求地址
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        // 请求参数
        let params = [
            "access_token": SSOAuthViewModel.share.accessToken,
            "since_id": "\(since_id)",
            "max_id": "\(max_id)"]
        
        
        // 发送请求
        request(type: .get, urlString: urlString, parameters: params, success: success, failure: failure)
    }
}
